package com.hi5.csocso.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.hi5.csocso.dao.UserDao;
import com.hi5.csocso.domain.User;

public class UserDaoHibernate implements UserDao {
    private SessionFactory sessionFactory;

    @Transactional
    public User getUser(int id) {
        return (User) this.sessionFactory.getCurrentSession()
                .createQuery("from User user where user.id=?")
                .setParameter(0, id).uniqueResult();
    }

    @Transactional
    public User getUser(String username, String passwordHash) {
        return (User) this.sessionFactory.getCurrentSession()
                .createQuery("from User user where user.username=? and user.password=?")
                .setParameter(0, username).setParameter(1, passwordHash)
                .uniqueResult();
    }

    @Transactional
    public List<User> getAllUser() {
        return this.sessionFactory.getCurrentSession()
                .createQuery("from User").list();
    }

    @Transactional
    public void saveUser(User user) {
        this.sessionFactory.getCurrentSession().saveOrUpdate(user);
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
